package com.chengyu.core.controller.shop;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.TrendsEnums;
import com.chengyu.core.domain.form.ShopSearchForm;
import com.chengyu.core.domain.form.TrendsForm;
import com.chengyu.core.domain.result.ShopCateResult;
import com.chengyu.core.domain.result.ShopWithCateResult;
import com.chengyu.core.domain.result.WalkTrendsResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.*;
import com.chengyu.core.service.member.MemberCollectShopService;
import com.chengyu.core.service.shop.ShopCateService;
import com.chengyu.core.service.shop.ShopConfigService;
import com.chengyu.core.service.shop.ShopFreightService;
import com.chengyu.core.service.shop.ShopService;
import com.chengyu.core.service.walk.WalkMemberService;
import com.chengyu.core.service.walk.WalkTrendsService;
import com.chengyu.core.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



@Api(tags = "店铺")
@Controller
@RequestMapping("/common")
public class ShopController extends UserBaseController {


	public static final Logger log = LoggerFactory.getLogger(ShopController.class);



	@Autowired
	private ShopService shopService;
	@Autowired
	private MemberCollectShopService memberCollectShopService;
	@Autowired
	private ShopConfigService shopConfigService;
	@Autowired
	private WalkTrendsService walkTrendsService;
	@Autowired
	private WalkMemberService walkMemberService;



	@Autowired
	private ShopCateService shopCateService;



	@Autowired
	private RedisUtil redisUtil;


	@Autowired
	private ShopFreightService shopFreightService;
//	@RequestMapping(value = "/hello/{id}")
//	public String hello(@PathVariable(value = "id") String id){
//		//查询缓存中是否存在
//		boolean hasKey = redisUtil.exists(id);
//		String str = "";
//		if(hasKey){
//			//获取缓存
//			Object object = redisUtil.get(id);
//			log.info("从缓存获取的数据"+ object);
//			str = object.toString();
//		}else{
//			//从数据库中获取信息
//			log.info("从数据库中获取数据");
//			str = testService.test();
//			//数据插入缓存（set中的参数含义：key值，user对象，缓存存在时间10（long类型），时间单位）
//			redisUtil.set(id,str,10L,redisUtil.MINUTES);
//			log.info("数据插入缓存" + str);
//		}
//		return str;
//	}



	@ApiOperation(value = "店铺列表")
	@ResponseBody
	@RequestMapping(value="/shop/getList", method=RequestMethod.GET)
	public CommonResult<CommonPage<UmsShop>> getList(
			ShopSearchForm form,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
		form.setStatus(CommonConstant.YES_INT);
		List<UmsShop> list = shopService.getShopList(form, page, pageSize);
		return CommonResult.success(CommonPage.restPage(list));
	}



	@ApiOperation(value = "店铺列表 带分类信息")
	@ResponseBody
	@RequestMapping(value="/shop/getListWithCate", method=RequestMethod.GET)
	public CommonResult<CommonPage<ShopWithCateResult>> getListWithCate(
			ShopSearchForm form,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
		form.setStatus(CommonConstant.YES_INT);
		List<ShopWithCateResult> list = shopService.getShopWithCateList(form, page, pageSize);
		return CommonResult.success(CommonPage.restPage(list));
	}

	@ApiOperation(value = "店铺列表 带店铺分类")
	@ResponseBody
	@RequestMapping(value="/shop/getListWithShopCate", method=RequestMethod.GET)
	public CommonResult<CommonPage<ShopWithCateResult>> getListWithShopCate(
			ShopSearchForm form,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
		form.setStatus(CommonConstant.YES_INT);
		List<ShopWithCateResult> list = shopService.getShopWithShopCateList(form, page, pageSize);
		return CommonResult.success(CommonPage.restPage(list));
	}



	@ApiOperation(value = "传参商品分类id 店铺列表 带店铺分类")
	@ResponseBody
	@RequestMapping(value="/shop/getShopWithCateListByCateId", method=RequestMethod.GET)
	public CommonResult<CommonPage<ShopWithCateResult>> getShopWithCateListByCateId(
			ShopSearchForm form,
//			@RequestParam(value = "cateId", defaultValue = "0") Integer cateId,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {

		form.setStatus(CommonConstant.YES_INT);
		List<ShopWithCateResult> list = shopService.getShopWithCateListByCateId(form, page, pageSize);
		return CommonResult.success(CommonPage.restPage(list));
	}







//	@Override
//	public List<ShopCateResult> getShopCateServiceResults(Integer shopId, Integer status) {
//		UmsShopCateExample example = new UmsShopCateExample();
//		UmsShopCateExample.Criteria criteria = example.createCriteria();
//		example.setOrderByClause("sort asc");
//
//		criteria.andTypeEqualTo(2);
//		criteria.andLevelEqualTo(1).andShopIdEqualTo(shopId);
//		if(status != null){
//			criteria.andStatusEqualTo(status);
//		}
//		List<UmsShopCate> topCateList = shopCateMapper.selectByExample(example);
//
//		List<ShopCateResult> cateResultList = new ArrayList<>();
//		for(UmsShopCate topCate : topCateList){
//			ShopCateResult result = new ShopCateResult();
//			BeanUtil.copyProperties(topCate, result);
//
//			example = new UmsShopCateExample();
//			criteria = example.createCriteria();
//			criteria.andLevelEqualTo(2).andShopIdEqualTo(shopId).andTypeEqualTo(2);
//			if(status != null){
//				criteria.andStatusEqualTo(status);
//			}
//			criteria.andPidEqualTo(topCate.getId());
//			example.setOrderByClause("sort asc");
//			List<UmsShopCate> childCateList = shopCateMapper.selectByExample(example);
//
//			List<ShopCateResult> twoCateList = new ArrayList<>();
//			for(UmsShopCate twoCate : childCateList){
//				ShopCateResult result1 = new ShopCateResult();
//				BeanUtil.copyProperties(twoCate, result1);
//
//				example = new UmsShopCateExample();
//				criteria = example.createCriteria();
//				criteria.andLevelEqualTo(3).andShopIdEqualTo(shopId).andTypeEqualTo(2);
//				criteria.andPidEqualTo(twoCate.getId());
//				if(status != null){
//					criteria.andStatusEqualTo(status);
//				}
//				example.setOrderByClause("sort asc");
//				List<UmsShopCate> threeChildCateList = shopCateMapper.selectByExample(example);
//				result1.setTopId(topCate.getId());
//
//				if(CollectionUtil.isNotEmpty(threeChildCateList)){
//					List<ShopCateResult> threeCateList = new ArrayList<>();
//					for(UmsShopCate threeCate : threeChildCateList){
//						ShopCateResult result2 = new ShopCateResult();
//						BeanUtil.copyProperties(threeCate, result2);
//						result2.setTopId(topCate.getId());
//						threeCateList.add(result2);
//					}
//					if (!threeCateList.isEmpty()) {
//						result1.setChildren(threeCateList);
//					}
//
//				}
//				twoCateList.add(result1);
//			}
//			if (!twoCateList.isEmpty()) {
//				result.setChildren(twoCateList);
//			}
//			cateResultList.add(result);
//		}
//		return cateResultList;
//	}



	@ApiOperation(value = "店铺详情")
	@ResponseBody
	@RequestMapping(value="/shop/get", method=RequestMethod.GET)
	public CommonResult<Map<String, Object>> getList(Integer shopId) {
		UmsShop shop = shopService.getShopById(shopId);
		Map<String, Object> map = BeanUtil.beanToMap(shop);
		//是否关注此店铺
		map.put("isCollectShop", memberCollectShopService.isCollectShop(getCurrentMemberOrNull(), shopId));
		System.out.println("11111111111111111111111");
		System.out.println(shopConfigService.getShopConfig(shopId));
		System.out.println("2222222222222222");
		map.put("cateStyle", shopConfigService.getShopConfig(shopId).getCateStyle());
		return CommonResult.success(map);
	}

	@ApiOperation(value = "店铺动态")
	@ResponseBody
	@RequestMapping(value="/shop/getTrendsList", method=RequestMethod.GET)
	public CommonResult<CommonPage<WalkTrendsResult>> search(Integer shopId, Integer page, Integer pageSize) {
		TrendsForm form = new TrendsForm();
		form.setStatus(CommonConstant.SUS_INT);
		form.setWalkMemberId(walkMemberService.getWalkMemberByMember(shopService.getShopById(shopId).getMemberId()).getId());
		form.setType(TrendsEnums.TrendsType.GOODS.getValue());
		CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(form, page, pageSize);
		return CommonResult.success(list);
	}

//	@ApiOperation(value = "店铺运费模板列表")
//	@ResponseBody
//	@RequestMapping(value="/shopFreight/getList", method=RequestMethod.GET)
//	public CommonResult<CommonPage<UmsShopFreightTemplate>> getList(
//			String name,
//			@RequestParam(value = "page", defaultValue = "1") Integer page,
//			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
//
//		List<UmsShopFreightTemplate> list = shopFreightService.getFreightTemplateList(getCurrentShop().getId(), name, page, pageSize);
//		return CommonResult.success(CommonPage.restPage(list));
//	}


}
